package com.ruoyi.biz.homepage.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.google.common.collect.Lists;
import com.ruoyi.biz.homepage.domain.HomePageVo;
import com.ruoyi.biz.letter.domain.BizCityLetter;
import com.ruoyi.biz.organization.domain.BizCompany;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 查询地市认领件统计
 *
 * @author Fei.Cong
 * @date 2023-04-21
 */
public interface IHomePageService {

    /**
     * 查询地市认领件统计
     */
    List<HomePageVo> totallist(BizCityLetter bizCityLetter);

    default void buildTotalCategory(List<HomePageVo> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            HomePageVo homePageVo = new HomePageVo();
            homePageVo.setCityCode(0L);
            homePageVo.setCityName("");
            homePageVo.setDeptId(0L);
            homePageVo.setDeptName("总计");
            homePageVo.setMergeRows(2L);
            homePageVo.setYouliNum(list.stream().mapToLong(HomePageVo::getYouliNum).sum());
            homePageVo.setWuliNum(list.stream().mapToLong(HomePageVo::getWuliNum).sum());
            homePageVo.setYlhjNum(list.stream().mapToLong(HomePageVo::getYlhjNum).sum());
            homePageVo.setDqajNum(list.stream().mapToLong(HomePageVo::getDqajNum).sum());
            homePageVo.setQuerenNum(list.stream().mapToLong(HomePageVo::getQuerenNum).sum());
            homePageVo.setDqajbjNum(list.stream().mapToLong(HomePageVo::getDqajbjNum).sum());
            homePageVo.setWuliTypeNum(list.stream().mapToLong(HomePageVo::getWuliTypeNum).sum());
            homePageVo.setYouliTypeNum(list.stream().mapToLong(HomePageVo::getYouliTypeNum).sum());
//            Map<String, List<HomePageVo>> deptNameMap = list.stream().collect(Collectors.groupingBy(HomePageVo::getCityName));
//            Long jiaoban = 0L;
//            Long tuihui = 0L;
//            for (Map.Entry<String, List<HomePageVo>> stringListEntry : deptNameMap.entrySet()) {
//                jiaoban += stringListEntry.getValue().get(0).getJiaobanNum();
//                tuihui += stringListEntry.getValue().get(0).getTuihuiNum();
//            }
//            homePageVo.setJiaobanNum(jiaoban);
//            homePageVo.setTuihuiNum(tuihui);
            homePageVo.setJiaobanNum(list.stream().mapToLong(HomePageVo::getJiaobanNum).sum());
            homePageVo.setTuihuiNum(list.stream().mapToLong(HomePageVo::getTuihuiNum).sum());
            homePageVo.setDqbjPercent("");
            homePageVo.setHjPercent("");
            List<Long[]> categoryTotal = Lists.newArrayList();
            for (HomePageVo v1 : list) {
                categoryTotal.add(v1.getCategoryList().stream().toArray(Long[]::new));
            }
            totalCategory(categoryTotal, homePageVo);
            list.add(homePageVo);
        }
    }

    default void totalCategory(List<Long[]> categoryTotal, HomePageVo homePageVo) {
        Long[] result = new Long[categoryTotal.get(0).length];
        for (Long[] total : categoryTotal) {
            for (int i = 0; i < result.length; i++) {
                result[i] = NumberUtil.add(result[i], total[i]).longValue();
            }
        }
        homePageVo.setCategoryList(Arrays.stream(result).collect(Collectors.toList()));
    }

}
